package com.haibei.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haibei.R;
import com.haibei.fragment.MenuFragment;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class BaseSlideActivity extends SlidingFragmentActivity {

	protected Fragment mFrag;
	protected SlidingMenu slidingMenu;
	public Dialog mProgressDialog = null;
	private ProgressBar mProgressBar = null;
	private AsyncTask mTaskRunning;
	private boolean needReCreate = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		crateDialog();
		getmProgressDialog().setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				if (null != mTaskRunning && !mTaskRunning.isCancelled()) {
					mTaskRunning.cancel(true);
				}
			}
		});
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		initView();

		setBehindContentView(R.layout.menu_frame);
		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
		mFrag = new MenuFragment();
		t.replace(R.id.menu_frame, mFrag);
		t.commit();

		slidingMenu = getSlidingMenu();
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setShadowWidth(getWindowManager().getDefaultDisplay().getWidth() / 40);
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		slidingMenu.setSecondaryShadowDrawable(R.drawable.right_shadow);
		slidingMenu.setBehindOffset(getWindowManager().getDefaultDisplay().getWidth() / 5);
		slidingMenu.setFadeEnabled(true);
		slidingMenu.setFadeDegree(0.4f);
		slidingMenu.setBehindScrollScale(0);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
	}

	/**
	 * 初始化视图
	 */
	public void initView() {

	}

	public void showProgressDialogWithTask(AsyncTask runingTask) {
		mTaskRunning = runingTask;
		showProgressDialog();
	}

	public void showProgressDialog() {
		Dialog dlg = getmProgressDialog();
		if (null != dlg) {
			dlg.show();
		} else {
			crateDialog();
			getmProgressDialog().show();
		}
	}

	public Dialog getmProgressDialog() {
		return mProgressDialog;
	}

	public void setmProgressDialog(Dialog mProgressDialog) {
		this.mProgressDialog = mProgressDialog;
	}

	public void crateDialog() {
		mProgressDialog = new Dialog(this, R.style.dialog);
		View dialogView = View.inflate(this, R.layout.dialog_progress, null);
		mProgressBar = (ProgressBar) dialogView.findViewById(R.id.progressBarMore);
		mProgressDialog.setContentView(dialogView);
		mProgressDialog.setCancelable(true);
	}

	public void hideProgressDialogWithTask() {
		if (null != mTaskRunning && !mTaskRunning.isCancelled()) {
			mTaskRunning.cancel(true);
		}
		hideProgressDialog();
	}

	/**
	 * 丛可输入的组件中获取输入
	 * 
	 * @param id
	 * @return
	 */
	public String getInputFromId(int id) {
		EditText edit = findEditTextById(id);
		String input = edit.getText().toString();
		return input;
	}

	public String getInput(EditText input) {
		return input.getText().toString();
	}

	public TextView findTextViewById(int id) {
		return (TextView) findViewById(id);
	}

	public EditText findEditTextById(int id) {
		return (EditText) findViewById(id);
	}

	public Button findButtonById(int id) {
		return (Button) findViewById(id);
	}

	public ImageView findImageViewById(int id) {
		return (ImageView) findViewById(id);
	}

	public ImageButton findImageButtonById(int id) {
		return (ImageButton) findViewById(id);
	}

	public ListView findListViewById(int id) {
		return (ListView) findViewById(id);
	}

	public RelativeLayout findRelativeLayout(int id) {
		return (RelativeLayout) findViewById(id);
	}

	public LinearLayout findLinearLayout(int id) {
		return (LinearLayout) findViewById(id);
	}

	public AutoCompleteTextView findAutoCompleteTextById(int id) {
		return (AutoCompleteTextView) findViewById(id);
	}

	@Override
	public void onBackPressed() {
		return;
	}

	public void showProgressDialog(boolean isCancellAble) {
		if (!isFinishing()) {
			if (isCancellAble) {
				mProgressDialog.setCancelable(true);
			} else {
				mProgressDialog.setCancelable(false);
			}
			mProgressDialog.show();
		}
	}

	public void hideProgressDialog() {
		mProgressDialog.hide();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(null!=mProgressDialog){
			mProgressDialog.dismiss();
		}
	}
}
